#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include "app_init.h"
#include "cmsis_os2.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "errno.h"
#include "errcode.h"
#include "pinctrl.h"
#include "common_def.h"

#include "gpio.h"
#include "adc.h"
#include "adc_porting.h"
#include "errno.h"
#include "gpio.h"

#include "auto_light.h"
#include "sensor.h"
#include "common_config.h"

void AutoLightContrl(uint16_t value)
{
    // printf("in ADC value = %d\n",(int)value);
    if (value < 1000) {
        printf("gpio low\n");
        uapi_gpio_set_val(LED_GPIO0_PIN,GPIO_LEVEL_LOW);
    } 
    else {
        printf("gpio high\n");
        uapi_gpio_set_val(LED_GPIO0_PIN, GPIO_LEVEL_HIGH);
    }
}

void AutoLightTask(void)
{
    uint16_t value;
    // while (1) {
    if (adc_port_read(LIGHT_GPIO12_ADC_CHANNEL, &value) != IOT_SUCCESS) {
        printf("ADC read error!\n");
    } else {
        printf("ADC_VALUE = %u\n", (unsigned int)value);
        data_set_light((int)value);
        AutoLightContrl(value);
        osDelay(50);
    }
    // }
}

void AutoLightInit(void) {
    //LED灯init
    uapi_adc_init(ADC_CLOCK_NONE);
    uapi_pin_set_mode(LED_GPIO0_PIN, LED_GPIO_PIN_MODE);
    uapi_gpio_set_dir(LED_GPIO0_PIN, GPIO_DIRECTION_OUTPUT);
    uapi_gpio_set_val(LED_GPIO0_PIN, GPIO_LEVEL_LOW);
}